#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <string>
using namespace std;

int main()
{
    //创建监听套接字
    int listen_sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(listen_sockfd < 0)
    {
        perror("socket");
        return 0;
    }
    //连接
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(28989);
    addr.sin_addr.s_addr = inet_addr("0.0.0.0");
    int ret = bind(listen_sockfd,(struct sockaddr*)&addr,sizeof(addr));
    if(ret < 0)
    {
        perror("bind");
        return 0;
    }
    //监听
    ret = listen(listen_sockfd,5);
    if(ret < 0)
    {
        perror("listen");
        return 0;
    }

    //创建epoll操作句柄
    int epoll_fd = epoll_create(5);
    if(epoll_fd < 0)
    {
        perror("epoll_create");
        return 0;
    }
    //将侦听套接字加入到红黑树中 进行监控
    struct epoll_event ee;
    ee.data.fd = listen_sockfd;
    ee.events = EPOLLIN;
    epoll_ctl(epoll_fd,EPOLL_CTL_ADD,listen_sockfd,&ee); 

    while(1)
    {
        //创建事件结构数组
        struct epoll_event arr[10];
        int ret = epoll_wait(epoll_fd,arr,10,-1);//监控 阻塞
        if(ret < 0)
        {
            perror("epoll_wait");//没监控成功
            continue;
        }
        else if(ret == 0)//超时
        {
            continue;
        }
        //epoll监控文件描述符，有对应就绪事件产生
        for(int i = 0; i < ret; i++)
        {
            if(arr[i].data.fd == listen_sockfd)
            {
                //如果是侦听套接字  accept接收创建新套接字
                int new_sockfd = accept(listen_sockfd,NULL,NULL);
                if(new_sockfd < 0)
                {
                    perror("accept");
                    continue;
                }
                //把新套接字给epoll监听起来
                //设置为非阻塞模式
                int flag = fcntl(new_sockfd,F_GETFL);
                fcntl(new_sockfd,F_SETFL,flag | O_NONBLOCK);
               
                struct epoll_event new_ee;
                new_ee.data.fd = new_sockfd;
                new_ee.events = EPOLLIN | EPOLLET;//设置为ET模式
                epoll_ctl(epoll_fd,EPOLL_CTL_ADD,new_sockfd,&new_ee);
            }
            else 
            {
                //如果不是侦听套接字
                int new_sockfd = arr[i].data.fd;
                string recv_buf;
                int flag = true;
                
                while(1)
                {
                    char buf[2] = {0};
                    ssize_t recv_size = recv(new_sockfd,buf,sizeof(buf)-1,0);
                    if(recv_size < 0)
                    {
                        if(errno == EAGAIN || errno == EWOULDBLOCK)
                        {
                            flag = true;
                            break;
                        }
                        perror("recv");
                        sleep(1);
                        break;
                    }
                    else if(recv_size == 0)
                    {
                        printf("shutdown connect");
                        epoll_ctl(epoll_fd,EPOLL_CTL_DEL,new_sockfd,NULL);
                        close(new_sockfd);
                        flag = false;
                        break;
                    }
                    recv_buf.append(buf);

                 }
                if(flag == true)
                {
                    printf("%s\n",recv_buf.c_str());
                    char buf[1024] = {0};
                    memset(buf,'\0',sizeof(buf));
                    strcpy(buf,"i am server");
                    send(new_sockfd,buf,sizeof(buf)-1,0);

                }
            }
        }

    }
    epoll_ctl(epoll_fd,EPOLL_CTL_DEL,listen_sockfd,NULL);
    close(listen_sockfd);
    return 0;
}
